package org.bookie.web.pages.admin;

import java.util.List;

import org.apache.tapestry.IAsset;
import org.apache.tapestry.annotations.Asset;
import org.apache.tapestry.annotations.InjectComponent;
import org.apache.tapestry.annotations.InjectObject;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.dojo.html.Dialog;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.form.BeanPropertySelectionModel;
import org.apache.tapestry.form.IPropertySelectionModel;
import org.bookie.framework.CommonBasePage;
import org.bookie.framework.components.PeriodComponent;
import org.bookie.model.Bet;
import org.bookie.model.Category;
import org.bookie.model.Event;
import org.bookie.model.Role;
import org.bookie.model.security.Access;
import org.bookie.model.services.CategoryDAO;
import org.bookie.model.services.EventDAO;
import org.bookie.web.pages.Results;

@Access(role = Role.ADMIN)
public abstract class EventList extends CommonBasePage implements PageBeginRenderListener{
	
	/**Identifier of the client component containing the period component.*/
	public static final String PERIOD = "period";
	
	public void pageBeginRender(PageEvent event) {
		PeriodComponent period = (PeriodComponent) getComponent(Results.PERIOD);
		List<Event> events = getEventDAO().find(period.getPeriod(), getCategory());
		setEvents(events);
	}
	
	public IAsset getStatusIcon(Event event){
		if(event.getState().equals(Event.State.ACTIVE)){
			return getTickIcon();
		}
		return getCrossIcon();
	}

	/**Injected update dialog*/
	@InjectComponent("cancelDialog")
	public abstract Dialog getCancelDialog();
	/**
	 * Cancels {@link Event}.
	 * @param eventId identifier of the event to cancel
	 */
	public void onCancelEvent(Long eventId){
		setEventId(eventId);
		getCancelDialog().show();
	}
	
	/**
	 * Cancels {@link Event}.
	 * @param eventId identifier of the event to cancel
	 */
	public void doCancelEvent(){
		getEventDAO().cancel(getEventId());
		setEventId(null);
		setMessage(getMessages().getMessage("admin.eventlist.cancel.message"));
		getCancelDialog().hide();
	}
	
	/**
	 * Returns <code>true</code> if the Event argument is cancelable,
	 * <code>false</code> otherwise.  
	 * @param event event to check 
	 */
	public boolean isCancelable(Event event){
		for (Bet bet: event.getBets()) {
			if(bet.getSuccessfulOutcome()==null){
				return true;
			}
		}
		return false;
	}
	
    /**
     * Get the selection model for {@link Category} options 
     * @return instance of IPropertySelectionModel
     */
	public IPropertySelectionModel getCategories(){
		return new BeanPropertySelectionModel(getCategoryDAO().findAll(), "name");
	}
	
	/**
	 * Get the selected {@link Category}
	 * @return instance of Category
	 */
	public abstract Category getCategory();
	
	/**
	 * Injected {@link EventDAO} service.
	 * @return instance of EventDAO
	 */
	@InjectObject("service:bookie.model.EventDAO")
	public abstract EventDAO getEventDAO();
	
	/**
	 * Injected {@link Category} service
	 * @return instance of CategoryDAO
	 */
	@InjectObject("service:bookie.model.CategoryDAO")
	public abstract CategoryDAO getCategoryDAO();
	
	/**Injected cross icon {@link IAsset}*/
	@Asset("images/cross.png")
	public abstract IAsset getCrossIcon();
	
	/**Injected tick icon {@link IAsset}*/
	@Asset("images/tick.png")
	public abstract IAsset getTickIcon();
	
	public abstract List<Event> getEvents();
	public abstract void setEvents(List<Event> events);
	
	@Persist
	public abstract Long getEventId();
	public abstract void setEventId(Long id);
}
